﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VPN.DataContext;
using System.Data.Linq;
using SWAS;

namespace VPN.Manage
{
    public class POManage
    {



        /// <summary>
        /// create a PO
        /// </summary>
        /// <param name="po"></param>
        public static void Create(PO po)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                pdc.POs.InsertOnSubmit(po);
                pdc.SubmitChanges();
            }
        }

        public static void Create(Account account, PO po)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                pdc.Accounts.InsertOnSubmit(account);
                pdc.POs.InsertOnSubmit(po);
                pdc.SubmitChanges();
            }
        }


        public static void Delete(Guid poID)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                PO po = pdc.POs.First(p => p.POID == poID);
                if (po == null)
                {
                    return;
                }
                else
                {
                    pdc.POs.DeleteOnSubmit(po);
                    pdc.SubmitChanges();
                }
            }
        }

        public static void Delete(string poNumber)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                PO po = pdc.POs.First(p => p.PONumber == poNumber);
                if (po == null)
                {
                    return;
                }
                else
                {
                    pdc.POs.DeleteOnSubmit(po);
                    pdc.SubmitChanges();
                }
            }
        }


        public static void Update(PO po)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                po.LastModifyDate = DateTime.Now;
                pdc.POs.Attach(po, true);
                pdc.SubmitChanges();
            }
        }

        public static PO[] GetPOList(POSearchArg arg, IPagerInfo pi)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadOnlyConnectionString))
            {
                pdc.ObjectTrackingEnabled = false;
                DataLoadOptions loads = new DataLoadOptions();
                loads.LoadWith<PO>(p => p.Product);
                pdc.LoadOptions = loads;

                var pos = pdc.POs.AsQueryable<PO>();
                if (!string.IsNullOrEmpty(arg.PONumber))
                    pos = pos.Where(p => p.PONumber.Contains(arg.PONumber));

                if (!string.IsNullOrEmpty(arg.UserName))
                    pos = pos.Where(p => p.UserName == arg.UserName);

                if (arg.AccountID != Guid.Empty)
                    pos = pos.Where(p => p.AccountID == arg.AccountID);

                if (arg.POStatus != POStatus.All)
                    pos = pos.Where(p => p.Status == (int)arg.POStatus);

                if (arg.CreateDateStart != null)
                    pos = pos.Where(p => p.CreateDate >= arg.CreateDateStart);

                if (arg.CreateDateEnd != null)
                    pos = pos.Where(p => p.CreateDate < arg.CreateDateEnd);

                if (arg.PayDateStart != null)
                    pos = pos.Where(p => p.PayClearDate >= arg.PayDateStart);

                if (arg.PayDateEnd != null)
                    pos = pos.Where(p => p.PayClearDate < arg.PayDateEnd);

                pi.DataCount = pos.Count();
                return pos.OrderByDescending(p => p.CreateDate).Skip(pi.PageIndex * pi.PageSize).Take(pi.PageSize).ToArray();
            }
        }

        public static PO GetPOByPONumber(string poNumber)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadOnlyConnectionString))
            {
                pdc.ObjectTrackingEnabled = false;
                DataLoadOptions loads = new DataLoadOptions();
                loads.LoadWith<PO>(p => p.Account);
                pdc.LoadOptions = loads;
                return pdc.POs.First(p => p.PONumber == poNumber);
            }
        }

        public static void ClearPay(string poNumber)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                PO po = pdc.POs.First(p => p.PONumber == poNumber);
                po.Account.Status = 0;
                po.Account.Type = po.Product.ProductCode;
                po.Account.ExpireDate = DateTime.Now.AddMonths(po.Product.Length).AddDays(1);
                po.HasPaid = true;
                pdc.SubmitChanges();
            }
        }


    }
}
